<?php
/**
 * Created by PhpStorm.
 * User: 董坤鸿
 * Date: 2017/1/12
 * Time: 下午6:29
 */

namespace EWeiLink\JuPai\Services;

use EWeiLink\JuPai\Contact;
use EWeiLink\JuPai\JuPai;
use EWeiLink\JuPai\Planner;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\Regex;

class PlannerRepo
{
    /**
     * 查看理财师
     * @param $id
     * @return Planner
     */
    public function find($id)
    {
        return Planner::find($id);
    }

    /**
     * 获取理财师列表
     * @param $filter
     * @param $page
     * @return mixed
     */
    public function getList($filter, $page)
    {
        $name = array_get($filter, 'name', null);
        if ($page) {
            return Planner::where('name', new Regex($name, 'gi'))->paginate(20);
        }
        $planner = Planner::where('name', new Regex($name, 'gi'))->get();
        $data['total'] = $planner->count();
        $data['page'] = 1;
        $data['per_page'] = $data['total'];
        $data['data'] = $planner;
        return $data;
    }

    /**
     * 获取指定理财师的联系人列表
     * @param $id
     * @return array
     */
    public function getContactsList($id)
    {
        $contact = Contact::where('plannerId', intval($id))->get();
        return $contact;
    }

    /**
     * 创建理财师
     * @param array $_options
     * @return Planner
     */
    public function create(array $_options) {
        $planner = new Planner();
        $planner->user_id = $_options['user_id'] ?? '';
        $planner->plannerId = (int) $_options['plannerId'];
        $planner->name = $_options['name'] ?? '';
        $planner->phone = $_options['phone'];
        $planner->branchInfo = $_options['branchInfo'] ?? '';
        $planner->validCode = $_options['validCode'] ?? getValidCode(1)[0];
        $planner->password = $_options['password']
            ?? bcrypt(JuPai::callBridgeHelper('createRandomStr', [6]));
        $planner->save();
        return $planner;
    }

    /**
     * 创建多个理财师
     * @param array $_options
     */
    public function createMany(array $_options) {
        $validCode = $this->getValidCode(count($_options));
        foreach ($_options as $key => $item) {
            $item['validCode'] = $validCode[$key];
            $this->create($item);
        }
    }

    /**
     * 生成特征码
     * @param int $count
     * @return array
     */
    public function getValidCode(int $count) {
        $i = 0;
        $validCode = [];
        while (count($validCode) !== $count) {
            while ($i++ < $count) {
                array_push($validCode, rand(10000, 99999));
            }
            if (count(array_unique($validCode)) === $count) {
                $re_code = Planner::whereIn('validCode', $validCode)
                    ->get()
                    ->pluck('validCode')
                    ->all();
                $validCode = array_diff($validCode, $re_code);
            }
            $i = count($validCode) - 1;
        }
        return $validCode;
    }
}
